﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Netnr.CodeBuilder
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_Test(object sender, RoutedEventArgs e)
        {
            string rt = Core.ConnectionTo.JoinConnection(this);
            if (rt != "")
            {
                try
                {
                    #region load database to combox
                    string sql = "SELECT name FROM Master..SysDatabases ORDER BY LEN(sid) ASC,name";
                    Core.DbHelperSQL.sqlConn = rt;
                    DataTable dt = Core.DbHelperSQL.GetDataTable(sql);
                    Con_Cbx_Database.Items.Clear();
                    foreach (DataRow dr in dt.Rows)
                    {
                        ComboBoxItem cbi = new ComboBoxItem();
                        cbi.Content = dr[0].ToString();
                        Con_Cbx_Database.Items.Add(cbi);
                    }
                    Con_Cbx_Database.SelectedIndex = 0;
                    #endregion
                    Con_Btn_OK.IsEnabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Fail （" + ex.Message + "）");
                }
            }
        }

        private void Button_Click_OK(object sender, RoutedEventArgs e)
        {
            string rt = Core.ConnectionTo.JoinConnection(this);
            if (rt != "")
            {
                Core.DbHelperSQL.sqlConn = rt;
                string ms = string.Empty;
                if (Core.ConnectionTo.TestConnection(rt))
                {
                    ms = "Connection Successful";
                    Con_Lbl_State.ToolTip = rt;
                }
                else
                {
                    ms = "Connection Failed";
                }
                Con_Lbl_State.Content = ms;
                MessageBox.Show(ms);
            }
        }

        private void Button_Click_ALL(object sender, RoutedEventArgs e)
        {
            Ec_List_Table.SelectAll();
        }

        private void Button_Click_UNALL(object sender, RoutedEventArgs e)
        {
            Ec_List_Table.SelectedItem = null;
        }

        private void Button_Click_Load(object sender, RoutedEventArgs e)
        {
            if (Core.DbHelperSQL.sqlConn.Length > 20)
            {
                #region load table to list
                string dbname = Con_Cbx_Database.Text;
                string sql = "SELECT name FROM " + dbname + "..SysObjects Where XType = 'U' ORDER BY Name";
                DataTable dt = Core.DbHelperSQL.GetDataTable(sql);
                Ec_List_Table.Items.Clear();
                foreach (DataRow dr in dt.Rows)
                {
                    ListBoxItem lbi = new ListBoxItem();
                    lbi.Content = dr[0].ToString();
                    Ec_List_Table.Items.Add(lbi);
                }
                #endregion
            }
            else
            {
                MessageBox.Show("Connection OK ?");
            }
        }

        private void Button_Click_Builder(object sender, RoutedEventArgs e)
        {
            var listTables = Ec_List_Table.SelectedItems;
            int fail = 0;

            var listUse = new string[listTables.Count];
            int index = 0;

            foreach (ListBoxItem table in listTables)
            {
                Core.BuilderTo.BuilderModel bm = new Core.BuilderTo.BuilderModel();

                bm.tableName = table.Content.ToString();

                bm.DomainNamespace = EC_Txt_Namespace.Text;
                if (string.IsNullOrEmpty(bm.DomainNamespace))
                {
                    bm.DomainNamespace = "Domain";
                }

                bm.MappingNamespace = EC_Txt_Namespace_Mapping.Text;
                if (string.IsNullOrEmpty(bm.MappingNamespace))
                {
                    bm.MappingNamespace = "Mapping";
                }

                bm.DomainClassName = bm.tableName;
                bm.MappingClassName = bm.tableName + "Mapping";
                bm.RepositoryClassName = bm.tableName + "Repository";

                bm.DomainFieldComment = EC_Chk_FieldComment.IsChecked == true ? true : false;
                bm.DomainFileName = bm.tableName;
                bm.DomainFileExt = ".cs";
                bm.DomainFileContent = Core.BuilderTo.BuilderContentDomain(bm);

                bm.MappingFileName = bm.tableName + "Mapping";
                bm.MappingFileExt = ".cs";
                bm.MappingFileContent = Core.BuilderTo.BuilderContentMapping(bm);

                bm.RepositoryFileName = "RepositoryUse";
                bm.RepositoryFileExt = ".cs";
                bm.RepositoryFileContent = Core.BuilderTo.BuilderContentRepository(bm);

                listUse[index++] = bm.RepositoryFileContent;

                if (!Core.BuilderTo.BuilderDomain(bm))
                {
                    fail++;
                }
                if (!Core.BuilderTo.BuilderMapping(bm))
                {
                    fail++;
                }

                if (listTables.Count == index)
                {
                    Core.BuilderTo.BuilderUse(bm, listUse);
                }
            }

            MessageBox.Show("Success rate：" + (listTables.Count - fail) + " / " + listTables.Count);

            ThreadPool.QueueUserWorkItem(_ =>
            {
                System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory);
            });
        }

        private void GoGit_MouseDownClick(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Process.Start(HP_lbl_git.Content.ToString());
        }
    }
}
